import 'package:get/get.dart';
import 'package:getx_example/get_connect/model/my_get_connect_model.dart';
import 'package:getx_example/get_connect/my_service/my_get_service.dart';

class MyGetConnectStateMixinController extends GetxController with StateMixin<List<MyGetConnectModel>> {
  final MyGetConnectStateMixinService connectStateMixinService = Get.find();

  @override
  void onInit() {
    // 调用
    fetchList();
    super.onInit();
  }

  // 拉取数据列表
  Future<void> fetchList() async {
    // 获取数据, 也可以处理好处理传过来,
    final Response response = await connectStateMixinService.getContent();
    // change(null, status: RxStatus.error(response.statusText));
    // change(null, status: RxStatus.empty());

    // return;
    // 判断，如果有错误
    if (response.hasError) {
      // 改变数据，传入错误状态，在ui中会处理这些错误
    } else {
      // 否则，存储数据，改变状态为成功
      List<MyGetConnectModel> dataList = [];
      for (var i = 0; i < 10; i++) {
        MyGetConnectModel getConnectModel = MyGetConnectModel(userId: 12312, id: 231231, title: "title", body: "body");

        dataList.add(getConnectModel);
      }
      change(dataList, status: RxStatus.success());
    }
  }
}

class MyGetContentStateMixinBuinding extends Bindings {
  @override
  void dependencies() {
    Get.lazyPut(() => MyGetConnectStateMixinService());
    Get.lazyPut(() => MyGetConnectStateMixinController());
  }
}


